<?php

namespace app\admin\controller\user;

use addons\wanlshop\library\WanlPay\WanlPay;
use app\common\controller\Backend;
use WanlPay\Yansongda\Pay;

/**
 * 购买会员记录
 *
 * @icon fa fa-circle-o
 */
class Order extends Backend
{

    /**
     * Order模型对象
     * @var \app\admin\model\user\UserOrder
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\user\UserOrder;
        $this->view->assign("statusList", $this->model->getStatusList());
    }



    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */


    /**
     * 查看
     */
    public function index()
    {
        //当前是否为关联查询
        $this->relationSearch = true;
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            $list = $this->model
                    ->with(['user','lvl'])
                    ->where($where)
                    ->order($sort, $order)
                    ->paginate($limit);

            foreach ($list as $row) {
                $row->visible(['id','member_day','price','order_time','payment_time','status']);
                $row->visible(['user']);
				$row->getRelation('user')->visible(['nickname']);
				$row->visible(['lvl']);
				$row->getRelation('lvl')->visible(['name']);
            }

            $result = array("total" => $list->total(), "rows" => $list->items());

            return json($result);
        }
        return $this->view->fetch();
    }


    //同意退款
    public function agree($ids)
    {
        $row =   $this->model->get($ids);
       if(in_array($row['status'],['1','3'])){
           $this->error('数据错误，请联系管理人员');
       }


        if($row['status'] == '4'){
            $this->error('已退款');
        }
        if($row['status'] != '2'){
            $this->error('数据错误');
        }


        if($row['status'] == '2'){
            $order['out_trade_no'] = $row->order_number; //商家订单号
            $order['out_refund_no'] ='HYTK'.date("Ymdhis") . sprintf("%08d", $this->auth->id) . mt_rand(10000, 99999) ;
            $order['total_fee'] = bcmul( $row->price, 100, 0); // 订单金额
            $order['refund_fee'] = bcmul($row->price, 100, 0);
            $order['refund_desc'] = '会员订单（订单号：'.$row->order_number.'）退款微信账户￥'.$row->price.'元。';
            $order['type'] = 'miniapp';
            $payConfig = (new WanlPay)->getConfig('wechat');
            $payConfig['notify_url'] = str_replace("notify", "notify_buy_member_refund", $payConfig['notify_url']);
            $wanlpay = Pay::wechat($payConfig)->refund($order);
            if($wanlpay->result_code == 'SUCCESS'){
                $this->success('提交成功');
            }
            $this->error('提交失败，请联系管理员');
        }
    }


}
